const express = require('express');
const app = express();
// 配置跨域
const cors=require('cors');
const joi = require('@hapi/joi')
const expressjwt = require('express-jwt');
const path = require('path');
const config = require('./config')
app.use(cors());
app.use('/uploads',express.static(path.join(__dirname,'./uploads')));
// 配合解析中间件
app.use(express.urlencoded({extended:false}));
// res.cc()封装用来显示信息
app.use((req,res,next) => {
    res.cc=function(err,status=1){
        res.send({
            status,
            message:err instanceof Error ? err.message:err  
        })
    }
    next();
})

// 解析token
app.use(expressjwt({ secret: config.jwtSecretKey }).unless({ path: [/^\/api\//] }))
// 引入user路由
const userRouter = require('./router/user')
app.use('/api',userRouter);
// 引入userinfo路由
const userinfoRouter=require('./router/userinfo');
app.use('/my',userinfoRouter);
app.use('/my/article',require('./router/artcate'));
app.use('/my/article',require('./router/article'));
app.use((err,req,res,next)=>{
    console.log(err);
    if (err instanceof joi.ValidationError) {
        return res.cc(err);
    }
    if(err.name=='UnauthorizedError') return res.cc('身份认证失败')
    res.cc(err);
})
app.listen(80,function(){
console.log('http://localhost:80');
});